/**
 * @Title: PerferDealerFragment.java
 * @Description: 首选经销商
 * @author Mengting Lu
 * @date 2015年8月6日 下午3:48:39
 */
package com.shanghaionstar.discover.dealer;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.mag.dealer.PreferredDealerEntity;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.dealer.iview.IPreferDealerView;
import com.shanghaionstar.discover.dealer.presenter.PreferDealerPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * @author Mengting Lu
 * @ClassName: PerferDealerFragment
 * @Description: 首选经销商
 * @date 2015年8月6日 下午3:48:39
 */
public class PreferDealerFragment extends BaseFragment implements OnClickListener, IPreferDealerView {
    @FindViewById
    private TextView perfer_dealer, tv_address, no_perfer_dealer_tv, item_tv;
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private LinearLayout perfer_dealer_layout;
    @FindViewById
    private RelativeLayout phone_rl, no_perfer_dealer_rl;
    @FindViewById
    private Button modify_btn;
    @FindViewById
    private Button setting_btn;
    @FindViewById
    private ProgressLayout pn_progress;

    private TextView tv_phone;
    private BaseActivity parent;
    private PreferDealerPresenter presenter;


    @Override
    public int getLayoutId() {
        return R.layout.discover_perfer_dealer;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        parent = (BaseActivity) activity;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        if (isFirstLoad()) {
            presenter = new PreferDealerPresenter(parent.getScheduler());
            presenter.setView(this);
            presenter.getPreferDealer();
        }
    }

    private void initView() {
        tv_phone = (TextView) phone_rl.findViewById(R.id.content_tv);
        item_tv.setText(getString(R.string.option_mobile));
        btnBackClick(top_layout);
        if (!DeviceUtil.isChineseLanguage())
            modify_btn.setVisibility(View.GONE);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        top_layout = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isFirstLoad()) {
            presenter.getCacheDealer();
        }
    }

//    private void savePreferredDealer() {
//        modify_btn.setText(R.string.save);
//        if (preEntity != null && !CommonUtil.isNull(preEntity.getDealerName())) {
//            tv_address.setText(preEntity.getAddress());
//            tv_phone.setText(preEntity.getTelephone());
//            perfer_dealer.setText(preEntity.getDealerName());
//            perfer_dealer_layout.setVisibility(View.VISIBLE);
//            no_perfer_dealer_rl.setVisibility(View.GONE);
//        } else {
//            handleNoData();
//        }
//    }


    @OnClick({R.id.modify_btn, R.id.phone_rl})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modify_btn:
                String text = modify_btn.getText().toString();
                if (text != null && text.equalsIgnoreCase(getString(R.string.option_modify))) {
                    presenter.switchToChangePreferDealer();
                } else {
                    presenter.changePerferDealer();
                }
                break;
            case R.id.phone_rl:
                presenter.showCall();
                break;
            default:
                break;
        }
    }

    @Override
    public void setLoading(boolean isLoading) {
        pn_progress.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(parent, e);
    }

    @Override
    public void onReponseSuccess(Object object) {
//        BusinessUtil.sendReportByClickType(getActivity(),
//                FunctionNameFindId.change_prefer_dealer.getStringValue());
        AlertDialogUtil.showAlertDialog(getActivity(), R.string.dialog_warning, R.string.SB023_MSG001,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        back();
                    }
                });
    }

    @Override
    public Activity onActivity() {
        return parent;
    }

    @Override
    public void onGetPreferDealer(PreferredDealerEntity dealerEntity) {
        onGetDealer(dealerEntity);
    }

    private void onGetDealer(PreferredDealerEntity dealerEntity) {
        if (dealerEntity != null && !CommonUtil.isNull(dealerEntity.getDealerName())) {
            tv_address.setText(dealerEntity.getAddress());
            tv_phone.setText(dealerEntity.getTelephone());
            perfer_dealer.setText(dealerEntity.getDealerName());
            perfer_dealer_layout.setVisibility(View.VISIBLE);
            no_perfer_dealer_rl.setVisibility(View.GONE);
        } else {
            onGetPreferDealerFailed();
        }
    }

    @Override
    public void onGetCachePreferDealer(PreferredDealerEntity dealerEntity) {
        modify_btn.setText(R.string.save);
        onGetDealer(dealerEntity);
    }

    @Override
    public void onGetPreferDealerFailed() {
        perfer_dealer_layout.setVisibility(View.GONE);
        no_perfer_dealer_tv.setText(R.string.no_preferred_dealer_content);
        no_perfer_dealer_rl.setVisibility(View.VISIBLE);
        if (DeviceUtil.getLanguage().equalsIgnoreCase("zh")) {
//            top_layout.setOnRightTextClickLstener(getString(R.string.change_perfer_dealer),
//                    new OnClickListener() {
//                        @Override
//                        public void onClick(View arg0) {
//                            presenter.switchToChangePreferDealer();
//                        }
//                    });
            setting_btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    presenter.switchToChangePreferDealer();
                }
            });


            modify_btn.setVisibility(View.VISIBLE);
        } else {
            modify_btn.setVisibility(View.GONE);
        }
    }
}
